<?php
    require_once '../funciones.php';
    $con        = con_caweb();
    $soporte    =$_SESSION['caweb']['soporte'];
    $cliente            = $_REQUEST['cliente'];
	$page = $_REQUEST['page'];


	// get how many rows we want to have into the grid - rowNum parameter in the grid
	$limit = $_REQUEST['rows'];

	// get index row - i.e. user click to sort. At first time sortname parameter -
	// after that the index from colModel
	$sidx = $_REQUEST['sidx'];

	// sorting order - at first time sortorder
	$sord = $_REQUEST['sord'];

	// if we not pass at first time index use the first column for the index or what you want
	if(!$sidx) $sidx =1;


	// calculate the number of rows for the query. We need this for paging the result
        $sql = "SELECT count(facturasistema) as count FROM cabeceras_fac 
                    WHERE cliente = '$cliente' and factura > 0 AND estadocartera <> 'C' AND estado = 'V' ";
        $tmsql = $sql;
        $result = mysql_query($sql,$con) or die("Error SQL 3".mysql_error().$sql);
	$row = mysql_fetch_array($result,MYSQL_ASSOC);
	$count = $row['count'];

	// calculate the total pages for the query
	if( $count > 0 ) $total_pages = ceil($count/$limit);
	else $total_pages = 0;

	// if for some reasons the requested page is greater than the total
	// set the requested page to total page
	if ($page > $total_pages) $page=$total_pages;

	// calculate the starting position of the rows
	$start = $limit*$page - $limit;

	// if for some reasons start position is negative set it to 0
	// typical case is that the user type 0 for the requested page
	if($start <0) $start = 0;

	// the actual query for the grid data
        $sql    = "select sucursal,ajuste,factura,fechafactura,tipo,base,iva,total,estado,estadocartera,juridico,fechapago,valorpagado,retefuente,reteiva,reteica,otros,cuadre,observacioncartera from cabeceras_fac
                    WHERE cliente = '$cliente' and factura > 0 AND estadocartera <> 'C' AND estado = 'V'  ORDER BY $sidx $sord LIMIT $start , $limit";
        $facturas = mysql_query($sql,$con) or die("Error SQL 2".mysql_error().$sql);

	// we should set the appropriate header information
	if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
				  header("Content-type: application/xhtml+xml;charset=utf-8");
	} else {
			  header("Content-type: text/xml;charset=utf-8");
	}
	echo "<?xml version='1.0' encoding='utf-8'?>";
	echo "<rows>";
//        echo "<sql>".$tmsql."</sql>";
	echo "<page>".$page."</page>";
	echo "<total>".$total_pages."</total>";
	echo "<records>".$count."</records>";
	// be sure to put text data in CDATA
	while($row = mysql_fetch_array($facturas,MYSQL_ASSOC)) {
		echo "<row id='". $row[facturasistema]."'>";                
		echo "<cell>". $row["factura"]."</cell>";
                echo "<cell>". $row["sucursal"]."</cell>";
		echo "<cell>". $row["fechafactura"]."</cell>";
		echo "<cell>". $row["tipo"]."</cell>";
		echo "<cell>". number_format($row["base"],0, ',', '.')."</cell>";
		echo "<cell>". number_format($row["iva"],0, ',', '.')."</cell>";
		echo "<cell>". number_format($row["total"],0, ',', '.')."</cell>";
		echo "<cell>". $row["estado"]."</cell>";
		if ($row["estadocartera"] == 'A') echo "<cell>Abonada</cell>";
		else echo "<cell>". $row["estadocartera"] ."</cell>";
		echo "<cell>". $row["juridico"]."</cell>";
		echo "<cell>". $row["fechapago"]."</cell>";
		echo "<cell>". $row["valorpagado"]."</cell>";
		echo "<cell>". $row["retefuente"]."</cell>";
		echo "<cell>". $row["reteiva"]."</cell>";
		echo "<cell>". $row["reteica"]."</cell>";
		echo "<cell>". $row["otros"]."</cell>";
		echo "<cell>". $row["cuadre"]."</cell>";
		echo "<cell>". utf8_encode(htmlspecialchars($row["observacioncartera"]))."</cell>";
		echo "</row>";
	}
	echo "</rows>";

?>
